package jdbc;



import util.MysqlUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;

public class Login4 {

    public static void main(String[] args)throws Exception {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username = sc.nextLine();
        System.out.println("请输入密码:");
        String password = sc.nextLine();

        //点击登录
        Login4User login = login(username);
        if (login==null){
            System.out.println("用户不存在");
        }else {
            if (password.equals(login.getPassword())){
                System.out.println("登录成功");
            }else {
                System.out.println("登录失败，密码错误");
            }
        }

    }

    public static Login4User login(String username)throws Exception{

        Login4User user=null;

        Connection conn = MysqlUtil.getCon();
        String sql="select * from login where username=?";

        PreparedStatement ps = MysqlUtil.getPs(sql);
        ps.setString(1,username);

        ResultSet rs = MysqlUtil.getRs();

        //验证用户是否存在
        if (!rs.next()){
            return user;
        }

        int id = rs.getInt("id");
        String password1 = rs.getString("password");

         user = new Login4User(id, username, password1);

        return user;

    }

}
